package com.bd.scm.module.qywx.util;

import com.alibaba.fastjson.JSONObject;
import com.bd.scm.module.qywx.service.SendMessageService;
import com.bd.scm.module.qywx.thread.TokenThread;
import com.bd.scm.module.utils.qywx.WeiXinParamesUtil;
import com.bd.utils.wechat.message.send.*;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @desc : 消息推送之发送消息
 * @author: shirayner
 * @date : 2017-8-18 上午10:04:55
 */
public class SendMessageUtil {

    //1.发送文本消息
    @SuppressWarnings("static-access")
    public static void testSendTextMessage(String content, String touser) throws Exception {
        //0.设置消息内容
//          String content="你的快递已到，请携带工卡前往邮件中心领取。\n出发前可查看" +
//                  "<a href=\"http://work.weixin.qq.com\">邮件中心视频实况" +
//                  "</a>，聪明避开排队。";

        //1.创建文本消息对象
        TextMessage message = new TextMessage();
        //1.1非必需
        message.setTouser(touser);  //不区分大小写
        //textMessage.setToparty("1");
        //txtMsg.setTotag(totag);
        //txtMsg.setSafe(0);

        //1.2必需
        message.setMsgtype("text");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Text text = new Text();
        text.setContent(content);
        message.setText(text);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
//          String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
//          System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms = new SendMessageService();
        sms.sendMessage(TokenThread.accessToken.getToken(), message);
    }

    /**
     * 访客预约驳回通知: XXX(被访问人)，您好：您邀请 xxx(某公司) xxx(某访客) 于 xxx(开始日期) 至 xxx(结束日期) 到访舍弗勒 xxx(某基地).已被xxx(审批人)拒绝！拒绝原因：xxx(拒绝原因)
     * Rejection of reservation request
     * Dear XXX(被访问人), you invites  xxx(某访客), xxx(某公司), to Schaeffler xxx(某基地) from xxx(开始日期) to  xxx(结束日期) have been rejected by xxx(审批人)！Reason：xxx(拒绝原因).
     * 这是发送给被访人信息
     *
     * @param jsonObject
     * @throws Exception
     */
    //2.发送文本卡片消息
    public synchronized static void testSendTextcardMessage(JSONObject jsonObject) throws Exception {
        Integer isZh = jsonObject.getInteger("isZh");
        String title = "";
        String description = "";

        //1 中文
        if (1 == isZh) {
            //0.设置消息内容
            title = "访客预约驳回通知:";
            description = "<div class=\"normal\">\n" + jsonObject.getString("employeeName") + "，您好：\r\n</div><div class=\"normal\">" +
                    "您邀请  " + jsonObject.getString("visitCompany") + "  " + jsonObject.getString("visitName") + " 于 " + jsonObject.getString("visitDateBegin") + " 至 " + jsonObject.getString("visitDateEnd") + " 到访舍弗勒" + jsonObject.getString("visitBaseName") + ".</div><div class=\"normal\">" +
                    "已被" + jsonObject.getString("approveName") + "拒绝！</div><div class=\"highlight\">\r\n拒绝原因：" + jsonObject.getString("reason") + ".</div>";
        }

        //0 英文
        if (0 == isZh) {
            //0.设置消息内容
            title = "Rejection of reservation request:";
            description = "<div class=\"normal\">\n Dear，" + jsonObject.getString("employeeName") + "：\r\n</div><div class=\"normal\">" +
                    "you invites  " + jsonObject.getString("visitName") + "," + jsonObject.getString("visitCompany") + ", " + " to Schaeffler " + jsonObject.getString("visitBaseName") + " from " + jsonObject.getString("visitDateBegin") + " to " + jsonObject.getString("visitDateEnd") + ".</div><div class=\"normal\">" +
                    "have been rejected by " + jsonObject.getString("approveName") + " ！</div><div class=\"highlight\">\r\nReason：" + jsonObject.getString("reason") + ".</div>";
        }

        String url = WeiXinParamesUtil.getProperty("projectPath") + "/visitor/BookingDetail?orderId=" + jsonObject.getString("orderId") + "&userId=" + jsonObject.getString("employeeId") + "&isZh=" + isZh;

        //1.创建文本卡片消息对象
        TextcardMessage message = new TextcardMessage();
        //1.1非必需
        message.setTouser(jsonObject.getString("employeeNo"));  //不区分大小写
        //message.setToparty("1");
        //message.setTotag(totag);
        //message.setSafe(0);

        //1.2必需
        message.setMsgtype("textcard");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Textcard textcard = new Textcard();
        textcard.setTitle(title);
        textcard.setDescription(description);
        textcard.setUrl(url);
        message.setTextcard(textcard);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        //String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        //System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms = new SendMessageService();
        sms.sendMessage(TokenThread.accessToken.getToken(), message);
    }

    /**
     * 这是发送给审批人信息
     * 访客预约审批通知:xxx(创建时间)xxx(某公司) xxx(某访客) 已预约  xxx(开始日期) 至 xxx(结束日期)  到访舍弗勒xxx(某基地).访问人数:xxx(人数)
     *
     * @param jsonObject
     * @throws Exception
     */
    //2.发送文本卡片消息
    public synchronized static void testSendTextcardMessage2(JSONObject jsonObject) throws Exception {
        Integer isZh = jsonObject.getInteger("isZh");
        String title = "";
        String description = "";

        //1 中文
        if (1 == isZh) {
            //0.设置消息内容
            title = "访客预约审批通知:";
            description = "<div class=\"gray\">" + jsonObject.getString("createTime") + "</div>" +
                    "<div class=\"normal\">" + jsonObject.getString("visitCompany") +
                    "  " + jsonObject.getString("visitName") + " 已预约 " +
                    jsonObject.getString("visitDateBegin") + " 至 " +
                    jsonObject.getString("visitDateEnd") + " 到访舍弗勒" +
                    jsonObject.getString("visitBaseName") + ".</div><div class=\"normal\">" +
                    "访问人数: " + jsonObject.getString("visitFollowCount") + "</div>";
        }

        //0 英文
        if (0 == isZh) {
            //0.设置消息内容
            title = "New approval of reservation request:";
            description = "<div class=\"gray\">" + jsonObject.getString("createTime") + "</div>" +
                    "<div class=\"normal\">" + jsonObject.getString("visitCompany") +
                    "  " + jsonObject.getString("visitName") + " will visit to Schaeffler " +
                    jsonObject.getString("visitBaseName") + " from " +
                    jsonObject.getString("visitDateBegin") + " to " +
                    jsonObject.getString("visitDateEnd") + ".</div><div class=\"normal\">" +
                    "Visitors: " + jsonObject.getString("visitFollowCount") + "</div>";
        }

        String url = WeiXinParamesUtil.getProperty("projectPath") + "approveDetails?orderId=" + jsonObject.getString("orderId") + "&userId=" + jsonObject.getString("employeeId") + "&approveStatus=" + jsonObject.getString("approveStatus") + "&isZh=" + isZh;

        //1.创建文本卡片消息对象
        TextcardMessage message = new TextcardMessage();
        //1.1非必需
        message.setTouser(jsonObject.getString("employeeNo"));  //不区分大小写
        //message.setToparty("1");
        //message.setTotag(totag);
        //message.setSafe(0);

        //1.2必需
        message.setMsgtype("textcard");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Textcard textcard = new Textcard();
        textcard.setTitle(title);
        textcard.setDescription(description);
        textcard.setUrl(url);
        message.setTextcard(textcard);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        //String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        //System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms = new SendMessageService();
        sms.sendMessage(TokenThread.accessToken.getToken(), message);
    }

    //7.发送图文消息
    @Test
    public static void testSendNewsMessage(String fromUserName) throws Exception {

        //1.创建图文消息对象
        NewsMessage message = new NewsMessage();
        //1.1非必需
        message.setTouser(fromUserName);  //不区分大小写
        //textMessage.setToparty("1");
        //txtMsg.setTotag(totag);
        //txtMsg.setSafe(0);

        //1.2必需
        message.setMsgtype("news");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));
        //设置图文消息
        Article article1 = new Article();
        article1.setTitle("访客预约系统操作说明");
        article1.setDescription("访客预约系统内回复‘ 操作说明 ’获取此推送信息");
        article1.setPicurl(WeiXinParamesUtil.getProperty("projectPath") + "/logo001.jpg");
        article1.setUrl(WeiXinParamesUtil.getProperty("projectPath") + "/caozuoshuoming.html");

        List<Article> articles = new ArrayList<Article>();
        articles.add(article1);

        News news = new News();
        news.setArticles(articles);
        message.setNews(news);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken = TokenThread.accessToken.getToken();
        System.out.println("accessToken:" + accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms = new SendMessageService();
        sms.sendMessage(accessToken, message);
    }
}
